package com.hy.common.excel;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.hy.common.utils.StateUtil;
import com.hy.entity.dto.HyPropertySearchDTO;
import com.hy.entity.vo.HyPropertyVO;
import com.hy.service.HyPropertyService;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * excel百万数据导入监听
 * @author bmbfm
 */
@Component
public class EntityExcelExport extends AbstractExcelExport<HyPropertyVO>{

    @Resource
    private HyPropertyService hyPropertyService;

    /**
     * 小数据量数据导出excel
     */
    public void exportWithSmallData(HttpServletResponse response, String fileName, HyPropertySearchDTO hyPropertySearchDTO){
        List<List<String>> head = new ArrayList<List<String>>();
        head.add(Collections.singletonList("资产ID"));
        head.add(Collections.singletonList("单位名称"));
        head.add(Collections.singletonList("资产RFID"));
        head.add(Collections.singletonList("资产固定编码"));
        head.add(Collections.singletonList("资产名称"));
        head.add(Collections.singletonList("规格"));
        head.add(Collections.singletonList("型号"));
        head.add(Collections.singletonList("是否帐外资产"));
        head.add(Collections.singletonList("资产类别"));
        head.add(Collections.singletonList("存放区域"));
        head.add(Collections.singletonList("管理人"));
        head.add(Collections.singletonList("使用人"));
        head.add(Collections.singletonList("管理部门"));
        head.add(Collections.singletonList("使用部门"));
        head.add(Collections.singletonList("资产状态"));
        head.add(Collections.singletonList("创建时间"));
        head.add(Collections.singletonList("修改时间"));
        head.add(Collections.singletonList("EPC"));
        this.exportSmallData(response, fileName, head, hyPropertyService.findList(BeanUtil.beanToMap(hyPropertySearchDTO)));
    }

    /**
     * 百万数据导出excel
     */
    public void exportWithBigData(HttpServletResponse response, String fileName,HyPropertySearchDTO hyPropertySearchDTO){

        this.exportWithBigData(response, fileName, hyPropertySearchDTO.getHead(), BeanUtil.beanToMap(hyPropertySearchDTO),HyPropertyVO.class);
    }

    @Override
    protected Integer dataTotalCount(Map<String,Object> queryCondition) {//计算导出数据的总数
        return hyPropertyService.getTotalCount(queryCondition);
    }

    @Override
    protected Integer eachSheetTotalCount() {//每一个sheet存放的数据总数
        return 500000;
    }

    @Override
    protected Integer eachTimesWriteSheetTotalCount() {//每次写入sheet的总数
        return 5000;
    }

    @Override
    protected void buildDataList(List<HyPropertyVO> resultList, Map<String, Object> queryCondition,
                                 Integer pageNo, Integer pageSize) {


        for (HyPropertyVO propertyVO : hyPropertyService.listPage(queryCondition, pageNo, pageSize)) {
            propertyVO.setOutName(StateUtil.getOutState(propertyVO.getOutProperty()));
            propertyVO.setStateName(StateUtil.getPropertyState(propertyVO.getPropertyState().intValue()));
            resultList.add(propertyVO);
        }

//        if(CollUtil.isNotEmpty(entityDoList)){
//            entityDoList.forEach(item -> {
//                resultList.add(Arrays.asList(String.valueOf(item.getPropertyId()),String.valueOf(item.getUnitName()),
//                        String.valueOf(item.getPropertyRfid()),item.getPropertyCode(),String.valueOf(item.getPropertyName()),
//                        String.valueOf(item.getPropertySpecification()), StateUtil.getOutState(item.getOutProperty())
//                        ,item.getPropertyType(),item.getHoldPlace()==null?"暂无":item.getMgrName()+item.getHoldPlace(),item.getManagerName(),item.getUsedName(),item.getManagerDeptName(),item.getUsedDeptName()
//                        ,StateUtil.getPropertyState(item.getPropertyState().intValue()),item.getSupplier(),item.getEpc(),item.getBrand(),
//                        item.getGainMethod(), item.getVoucher(),item.getGainDate(),item.getEnterDate(),item.getCreateDate(),item.getUpdateDate()
//                        ,item.getAccountDate(),item.getOriginValue()
//
//                ));
//            });
//        }
    }
}
